//
//  LoginController.m
//  上拉刷新
//
//  Created by KM on 16/10/31.
//  Copyright © 2016年 KM. All rights reserved.
//

#import "LoginController.h"
#import "LoginData.h"
#import "ShuaxinController.h"

@interface LoginController ()

@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.password.secureTextEntry = YES;

}
- (IBAction)onClik:(id)sender {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/login.php?name=%@&password=%@",self.username.text,self.password.text]];
    
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSURLSession *se = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [se dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSString *text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        NSLog(@"%@",text);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        LoginData *d = [[LoginData alloc]initWithDictionary:dict];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (d.errcode == 0) {
                ShuaxinController *shuax = [self.storyboard instantiateViewControllerWithIdentifier:@"shua"];
                [self.navigationController pushViewController:shuax animated:YES];
            }
            else
            {
                NSLog(@"%@",d.errinfo);
            }

        });
    }];
    [task resume];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
